package com.dsp.inventory.dao;

import com.dsp.inventory.entity.Inventory;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

public interface InventoryDao extends PagingAndSortingRepository<Inventory,Long> {

    /**
     * 减库存
     */
    @Modifying
    @Query("update inventory set sum = sum - :num " +
            ",lock_sum = lock_sum + :num " +
            "where id = :id and sum>0 and sum >= :num")
    boolean decrease(@Param("id")Long id, @Param("num")Integer num);

    /**
     * 确认减库存(减的是锁定库存)
     */
    @Modifying
    @Query("update inventory set lock_sum = lock_sum - :num " +
            " where id =:id and lock_sum > 0 ")
    int confirm(@Param("id")Long id, @Param("num")Integer num);

    /**
     * 库存恢复
     */
    @Modifying
    @Query("update inventory set sum = sum + :num," +
            " lock_sum= lock_sum - :num " +
            " where id = :id  and lock_sum > 0 ")
    int cancel(@Param("id")Long id, @Param("num")Integer num);
}